package graduation.project.testcase.strategy;

import graduation.project.body.OriginExpression;
import graduation.project.body.TestCase;
import graduation.project.body.TestCases;
import graduation.project.exception.NonExpressionException;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

public class MUMCUTGenerator implements TestSetGenerator {

	public TestCases generateTestCases(String expression, int testCaseNumber) {
		try {
			OriginExpression originEx;
			originEx = new OriginExpression(expression);
			return generateTestCases(originEx, testCaseNumber);
		} catch (NonExpressionException e) {
			System.out.println("the expression is not valid boolean expression");
			e.printStackTrace();
		}
		return null;
	}

	public TestCases generateTestCases(OriginExpression expression,
			int testCaseNumber) {
		
		TestCases result = new TestCases(expression.getExpression(), expression.getModel(), "MUMCUT");
		ArrayList<TestCase> testSet = new ArrayList<TestCase>();
//		List temp;
		TestCases mutpTestSet = new MUTPGenerator().generateTestCases(expression, testCaseNumber);
		/*System.out.println("----------------------------mutp-----------------");
		if(mutpTestSet.getTestSet() != null && mutpTestSet.getTestSet().size() > 0){
			temp = mutpTestSet.getTestSet();
			for(int i = 0; i <temp.size(); i++){
				TestCase temp2 = (TestCase)temp.get(i);
				System.out.println("temp2: " + temp2.getTestData() + ", exp: " + temp2.getExpectantResult());
			}
		}
		System.out.println("----------------------------mnfp-----------------");*/
		TestCases mnfpTestSet = new MNFPGenerator().generateTestCases(expression, testCaseNumber);
		/*if(mnfpTestSet.getTestSet() != null && mnfpTestSet.getTestSet().size() > 0){
			temp = mnfpTestSet.getTestSet();
			for(int i = 0; i <temp.size(); i++){
				TestCase temp2 = (TestCase)temp.get(i);
				System.out.println("temp2: " + temp2.getTestData() + ", exp: " + temp2.getExpectantResult());
			}
		}
		System.out.println("----------------------------cup-----------------");*/
		TestCases cutpnfpTestSet = new CUTPNFPGenerator().generateTestCases(expression, testCaseNumber);
		/*if(cutpnfpTestSet.getTestSet() != null && cutpnfpTestSet.getTestSet().size() > 0){
			temp = cutpnfpTestSet.getTestSet();
			for(int i = 0; i <temp.size(); i++){
				TestCase temp2 = (TestCase)temp.get(i);
				System.out.println("temp2: " + temp2.getTestData() + ", exp: " + temp2.getExpectantResult());
			}
		}*/
		LinkedHashSet<TestCase> hs = new LinkedHashSet<TestCase>();
		hs.addAll(mutpTestSet.getTestSet());
		hs.addAll(mnfpTestSet.getTestSet());
		hs.addAll(cutpnfpTestSet.getTestSet());
		
/*		System.out.println("-------------------------MumCut Bigin------------------------");
		System.out.println("expression: " + expression.getExpression() + ", idnf: " + expression.getIDNF() + ", model: " + expression.getModel());
		System.out.println("hs.size(): " + hs.size());
		Iterator<TestCase> it = hs.iterator();
		while(it.hasNext()){
			TestCase tc = it.next();
			System.out.println("test data: " + tc.getTestData() + ", expectant: " + tc.getExpectantResult());
		}
		System.out.println("-------------------------MumCut End------------------------");*/
		
		testSet.addAll(hs);
		result.setTestSet(testSet);
		
		return result;
	}

}
